﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

using Wilke.Interactive.Drone.Control;

namespace Wilke.Interactive.Drone.PlayGround
{
    /// <summary>
    /// Interaction logic for MainControl.xaml
    /// </summary>
    public partial class Radar
    {
        public event EventHandler<DroneCoordinatesChangedEventArgs> DroneCoordinatesChanged;

        private bool droneIsActive = false;

        public Single Roll
        {
            set
            {
                Matrix matrix;
                MatrixTransform droneTransform = drone.RenderTransform as MatrixTransform;

                if (droneTransform != null)
                {
                    Single roll = value;
                    if (Math.Abs(roll) > 1)
                    {
                        value = 1 * Math.Sign(roll);
                    }
                    matrix = droneTransform.Matrix;
                    matrix.OffsetX = canvas.ActualWidth / 2 * roll;
                    droneTransform.Matrix = matrix;
                }
            }
        }

        public Single Pitch 
        {
            set
            {
                Matrix matrix;
                MatrixTransform droneTransform = drone.RenderTransform as MatrixTransform;

                if (droneTransform != null)
                {
                    Single pitch = value;
                    if (Math.Abs(pitch) > 1)
                    {
                        pitch = 1 * Math.Sign(pitch);
                    }
                    matrix = droneTransform.Matrix;
                    matrix.OffsetY = canvas.ActualHeight / 2 * pitch;
                    droneTransform.Matrix = matrix;
                }
            }
        }

        public Single Yaw
        {
            set
            {
                Matrix matrix;
                MatrixTransform droneTransform = drone.RenderTransform as MatrixTransform;

                if (droneTransform != null)
                {
                    Single yaw = value;
                    if (Math.Abs(yaw) > 1)
                    {
                        value = 1 * Math.Sign(yaw);
                    }
                    matrix = droneTransform.Matrix;
                    matrix.Rotate(yaw * 180);
                    sweepRotate.Angle = yaw * 180;
                    droneTransform.Matrix = matrix;
                }
            }
        }
     
        public Radar()
        {
            this.InitializeComponent();
        }

        private void UserControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            Matrix matrix;
            MatrixTransform droneTransform = drone.RenderTransform as MatrixTransform;

            if (droneTransform != null)
            {
                matrix = droneTransform.Matrix;

                if (droneIsActive)
                {
                    matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
                }
                else
                {
                    // Apply any deltas to the matrix,
                    // then apply the new Matrix as the MatrixTransform data:
                    
                    Point center = new Point(0, 0);
                    center = matrix.Transform(center);

                    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
                    sweepRotate.Angle += e.DeltaManipulation.Rotation;
                }
                droneTransform.Matrix = matrix;

                UpdateValues(matrix);
            }            

            e.Handled = true;
        }

        private void canvas_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            if (e.OriginalSource is Drone)
            {
                droneIsActive = true;
            }
            else
            {
                // Set the ManipulationPivot so that the element rotates as it is
                // moved with one finger.
                ManipulationPivot pivot = new ManipulationPivot();

                //pivot.Center = new Point(UserControl.ActualWidth / 2, UserControl.ActualHeight / 2);
                pivot.Center = new Point(0, 0);
                pivot.Radius = 100;
                e.Pivot = pivot;
                e.IsSingleTouchEnabled = true;
                e.Mode = ManipulationModes.Rotate;
                droneIsActive = false;
            }

            e.ManipulationContainer = this;
            e.Handled = true;
        }

        private void buttonUp_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void Canvas_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            CenterDrone();
        }

        private void CenterDrone()
        {
            Matrix matrix = ((MatrixTransform)drone.RenderTransform).Matrix;
            matrix.OffsetX = 0;
            matrix.OffsetY = 0;
            matrix.Rotate(-sweepRotate.Angle);
            drone.RenderTransform = new MatrixTransform(matrix);
            sweepRotate.Angle = 0;
            UpdateValues(matrix);
        }

        private void UpdateValues(Matrix matrix)
        {
            Single pitch = (float)(matrix.OffsetY / radarScreen.ActualHeight * 2);
            Single roll = (float)(matrix.OffsetX/radarScreen.ActualWidth * 2);

            if (DroneCoordinatesChanged != null)
            {
                float yaw = 0;

                if(sweepRotate.Angle > 0)
                {
                    yaw = 1;
                }
                else if(sweepRotate.Angle < 0)
                {
                    yaw = -1;
                }
                
                DroneCoordinatesChanged(this, new DroneCoordinatesChangedEventArgs(pitch, roll, yaw, 0));
            }
        }
    }
}